package com.enumDemo;

public enum Season {

    //枚举类型 不能通过 new 来实例化

    // Season.SPRING  就已经自动调用有参构造器 并传递参数  相当于    Season.SPRING = new  Season(1, "spring")

    // 通过构造函数传递参数并创建实例
    SPRING(1, "spring"),
    SUMMER(2, "summer"),
    AUTUMN(3, "autumn"),
    WINTER(4, "winter");

    // 定义实例对应的参数
    private Integer key;
    private String msg;

    // 必写：通过此构造器给枚举值创建实例
    Season(Integer key, String msg) {
        this.key = key;
        this.msg = msg;
    }

    // 很多情况，我们可能从前端拿到的值是枚举类的 key ，然后就可以通过以下静态方法获取到对应枚举值
    public static Season valueofKey(Integer key) {
        for (Season season : Season.values()) {
            if (season.key.equals(key)) {
                return season;
            }
        }
        throw new IllegalArgumentException("No element matches " + key);
    }

    // 通过此方法可以获取到对应实例的 key 值
    public Integer getKey() {
        return key;
    }

    // 通过此方法可以获取到对应实例的 msg 值
    public String getMsg() {
        return msg;
    }

    //通过 msg 获取 key
    public static Integer msgToKey(String msg) {
        for (Season season : Season.values()) {
            if (season.msg.equals(msg)) {
                return season.getKey();
            }
        }
        throw new IllegalArgumentException("No element matches " + msg);
    }


    //通过 key 获取  msg
    public static String keyToMsg(Integer key) {
        for (Season season : Season.values()) {
            if (season.key.equals(key)) {
                return season.getMsg();
            }
        }
        throw new IllegalArgumentException("No element matches " + key);
    }

}
